package com.example.gjk.web;

import com.example.gjk.dao.CompanyMapper;
import com.example.gjk.entity.Company;
import com.example.gjk.entity.CompanyInventoryStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("web")
public class WebController {
    @Autowired
    CompanyMapper companyMapper;

    @RequestMapping("sel")
    public String sel(ModelMap map) {
        List<Company> ACompany = companyMapper.selCompany("A");
        List<Company> AACompany = companyMapper.selCompany("AA");
        List<Company> AAJCompany = companyMapper.selCompany("AA+");
        List<Company> AAACompany = companyMapper.selCompany("AAA");
        map.put("ACompany", ACompany);
        map.put("AACompany", AACompany);
        map.put("AAJCompany", AAJCompany);
        map.put("AAACompany", AAACompany);
        return "index";
    }

    @RequestMapping("edit/{name}")
    public String edit(ModelMap map, @PathVariable("name") String companyName) {
        Company company = companyMapper.selEdit(companyName);
        map.put("companyName", companyName);
        map.put("company", company);
        return "update";
    }

    @RequestMapping("editBack")
    public String editBack(Company company, String companyName) {
        companyMapper.edit(company, companyName);
        return "redirect:/web/sel";
    }

    @RequestMapping("delete/{name}")
    public String delete(@PathVariable("name") String companyName) {
        companyMapper.delete(companyName);
        return "redirect:/web/sel";
    }

    @RequestMapping("add")
    public String add() {
        return "add";
    }

    @RequestMapping("addBack")
    public String addBack(Company company) {
        companyMapper.add(company);
        return "redirect:/web/sel";
    }

    @RequestMapping("sanlian/{Object}")
    public String replace(ModelMap map, @PathVariable("Object") String companyName) {
        List<CompanyInventoryStatus> MGood = companyMapper.selgood(companyName, "原材料");
        List<CompanyInventoryStatus> good = companyMapper.selgood(companyName, "制成品");
        map.put("MGood", MGood);
        map.put("Good", good);
        map.put("companyName", companyName);
        return "sanlian";
    }

    @RequestMapping("goodListDelete/{goodName}&{companyName}")
    public String goodListDelete(@PathVariable("goodName") String goodName, @PathVariable("companyName") String companyName) {
        companyMapper.deletegood(companyName, goodName);
        return "redirect:/web/sel";
    }

    @RequestMapping("goodListEdit/{goodName}")
    public String goodListEdit(@PathVariable("goodName") String goodName, ModelMap map1) {
        CompanyInventoryStatus good = companyMapper.goodSelSingle(goodName);
        map1.put("Name", good.getGoodName());
        map1.put("Good", good);
        return "sanlianUpdate";
    }

    @RequestMapping("sanlianEditBack")
    public String sanlianEditBack(CompanyInventoryStatus good, String Name) {
        companyMapper.goodEditSingle(good, Name);
        return "redirect:/web/sel";
    }

    @RequestMapping("Add")
    public String Add() {
        return "sanlianAdd";
    }

    @RequestMapping("sanlianAddBack")
    public String sanlianAddBack(CompanyInventoryStatus good) {
        companyMapper.goodAddSingle(good);
        return "redirect:/web/sel";
    }


    @PostMapping("sanlian/{Object}")
    @ResponseBody
    public Map sanlianData() {
        Map map = new HashMap();
        String company = "鲁泰纺织股份有限公司";
        ArrayList<CompanyInventoryStatus> companyGoodList = companyMapper.goodSel(company);
        String[] goodNameList = new String[companyGoodList.size()];
        for (int a = 0; a < companyGoodList.size(); a++) {
            CompanyInventoryStatus companyIS = companyGoodList.get(a);
            goodNameList[a] = companyIS.getGoodName();
        }
        List<Map> goodList = new ArrayList<>();//详细信息
        for (int i = 0; i < companyGoodList.size(); i++) {
            Map mm = new HashMap();
            mm.put("name", companyGoodList.get(i).getGoodName());
            mm.put("type", companyGoodList.get(i).getType());
            mm.put("data", new int[]{companyGoodList.get(i).getDay1(),
                    companyGoodList.get(i).getDay8(),
                    companyGoodList.get(i).getDay15()
            });
            goodList.add(mm);
        }
        map.put("goodNameList", goodNameList);//商品名称
        map.put("goodList", goodList);//商品列表
        System.out.println(goodList);
        System.out.println(goodNameList);
        return map;
    }
}
